package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.CommentsApi;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.SmallVideosApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ManageService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private SmallVideosApi smallVideosApi;
    @DubboReference
    private MovementApi movementApi;

    @DubboReference
    private CommentsApi commentsApi;
    //分页查询全部
    public PageResult users(Integer page, Integer pagesize) {
      IPage iPage = userInfoApi.findAll(page,pagesize);
      return new PageResult(page,pagesize,iPage.getTotal(),iPage.getRecords());
    }
    //查询用户详情
    public UserInfo findUserById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        return userInfo;
    }
    //查询视频列表
    public PageResult findVideos(Integer page, Integer pagesize, Long uid) {
        PageResult result  = smallVideosApi.findByUserId(uid,page, pagesize);
        //获取视频集合
        List<Video> list = (List<Video>) result.getItems();
        //判断是否为空
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }
        //查询用户信息
        UserInfo userInfo = userInfoApi.findById(uid);
        //转换为vo
        List<VideoVo> vos = new ArrayList<>();
        for (Video video : list) {
            VideoVo vo = VideoVo.init(userInfo, video);
            vos.add(vo);
        }
        result.setItems(vos);
        return result;

    }
    //查询动态列表
    public PageResult findMovements(Integer state, Long uid, Integer page, Integer pagesize) {
       PageResult result = movementApi.findMovements(state,uid,page,pagesize);
        List<Movement> items = (List<Movement>) result.getItems();
        if (CollUtil.isEmpty(items)){
            return new PageResult();
        }
        //查询用户信息
        List<Long> ids = CollUtil.getFieldValues(items, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(ids);
        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : items) {
            UserInfo userInfo = map.get(movement.getUserId());
            MovementsVo vo = MovementsVo.init(userInfo, movement);
            vos.add(vo);
        }
        result.setItems(vos);
        return result;
    }
    //根据id查询动态
    public MovementsVo findMovementById(String id) {
        Movement movement = movementApi.findById(id);
        UserInfo userInfo = userInfoApi.findById(movement.getUserId());
        MovementsVo vo = MovementsVo.init(userInfo, movement);
        return vo;
    }
    //根据动态id查询评论
    public PageResult findCommentsById(Integer page, Integer pagesize, String id) {
        PageResult result =  commentsApi.findCommentsById(id,page,pagesize);
        List<Comment> items = (List<Comment>) result.getItems();
        if (CollUtil.isEmpty(items)){
            return new PageResult();
        }
        List<Long> ids = CollUtil.getFieldValues(items, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(ids);
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : items) {
            Long userId = comment.getUserId();
            UserInfo userInfo = map.get(userId);
            CommentVo vo = CommentVo.init(userInfo, comment);
            vos.add(vo);
        }
        result.setItems(vos);
        return result;

    }
}
